using Core;
using System.Text;
using UnityEditor;
using UnityEngine;

public class ExportWndFields
{
    [MenuItem("Assets/Mini Tools/UI/Copy All UIWidgets", false, 1)]
    [MenuItem("GameObject/Mini Tools/GameObject/Copy All UIWidgets", false, 1)]
    private static void Main()
    {
        var go = Selection.activeGameObject;
        if (go == null)
        {
            return;
        }
        StringBuilder builder = new();

        builder.AppendLine(@"#pragma warning disable 0649");
        Search(go.transform, builder, string.Empty);
        builder.AppendLine(@"#pragma warning restore 0649");

        EditorGUIUtility.systemCopyBuffer = builder.ToString();
        Debug.Log("Copy All UIWidgets Complete");
    }

    private static void Search(Transform trans, StringBuilder builder, string path)
    {
        if (trans == null)
        {
            return;
        }
        var cnt = trans.childCount;
        if (cnt == 0)
        {
            return;
        }
        for (int i = 0; i < cnt; i++)
        {
            var child = trans.GetChild(i);
            var name = child.name;
            var strs = name.Split('_');

            string sub_path;
            if (path == string.Empty)
            {
                sub_path = name;
            }
            else
            {
                sub_path = $"{path}/{name}";
            }

            //if (strs.Length <= 1)
            //{
            //    Search(child, builder, sub_path);
            //    continue;
            //}

            var prefix = strs[0];
            switch (prefix)
            {
                case "go":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly Widget {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "img":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIImage {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "txt":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UILabel {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "btn":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIButton {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "sbtn":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UISkillButton {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "tgl":
                case "toggle":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIToggle {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "cnt":
                case "counter":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UICounter {name};");
                        break;
                    }
                case "slice":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UISlice {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "vs":
                case "viewslice":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIWidgetSlice {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "sv":
                case "scrollview":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIScrollView {name};");
                        break;
                    }
                case "slider":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UISlider{name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "ipt":
                case "inputfield":
                    {
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIInputField {name};");

                        Search(child, builder, sub_path);

                        break;
                    }
                case "grid":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIGrid {name};");
                        break;
                    }
                case "view":
                    {
                        Debug.Log(child.name);
                        string s = name.Replace("view_", "");
                        s = Utils.Text.ToUpper(s, 1);

                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UI{s}View {name};");

                        break;
                    }
                case "joystick":
                    {
                        Debug.Log(child.name);
                        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                        builder.AppendLine($"\t\tprivate readonly UIJoystick {name};");
                        break;
                    }
                //case "floatball":
                //    {
                //        builder.AppendLine($"\t\t[UIField(\"{sub_path}\")]");
                //        builder.AppendLine($"\t\tprivate readonly UIFloatingBall {name};");

                //        //Search(child, builder, sub_path);

                //        break;
                //    }
                default:
                    Search(child, builder, sub_path);
                    break;
            }
        }
    }


}
